package org.wak.gui.sci
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BitmapFilterType;
	import flash.ui.Mouse;
	
	import org.wak.action.actions.AdventureActions;
	import org.wak.gui.IAdventureGui;
	import org.wak.gui.events.GuiEvent;
	import org.wak.inventory.Inventory;
	import org.wak.inventory.InventoryItem;
	import org.wak.inventory.events.InventoryEvent;
	import org.wak.main.AdventureMaster;
	import org.wak.utils.Utils;
	import org.wak.utils.WakSimpleButton;


/**
	* Dispatched when an action button is clicked
	* 
	* @eventType org.wak.gui.events.GuiEvent.ACTION_SELECTED
	*/
	[Event(name="ACTION_SELECTED", type="org.wak.gui.events.GuiEvent")]
	
  /**
	* Dispatched when an inventory item button is selected
	* in the inventory window.
	* 
	* @eventType org.wak.gui.events.GuiEvent.INVENTORY_ITEM_SELECTED
	*/
	[Event(name="INVENTORY_ITEM_SELECTED", type="org.wak.gui.events.GuiEvent")]
	
	
	
	/**
	 * Manage the SCI graphic user interface.  
	 * 
	 */	
	public class SciGui extends Sprite implements IAdventureGui
	{
		
		/**
		 * Internal action for the button open inventory.
		 */		
		public static const OPEN_INVENTORY:String = "open_inventory" ;
		
		/**
		 * Internal action for the button close inventory.
		 */	
		public static const CLOSE_INVENTORY:String = "close_inventory" ;
		
		/**
		 * Internal inventory display action for the button select.
		 */	
		public static const SELECT_INVENTORY:String = "select_item" ;
		
		
		private var mEnabled:Boolean = true ;
		
		private var mBevelFilter1:BevelFilter = null ;
		
		// panel of buttons moving up or down.
		private var mButtonsPanel:Sprite = null
		
		private var mStageWidth:Number = 0 ;
		private var mStageHeight:Number = 0 ;
		
		// width of the default button.
		private var mDefaultButtonWidth:Number = 60 ;
		
		// height of the default button.
		private var mDefaultButtonHeight:Number = 45 ;
		
		// color of the default button.
		private var mDefaultButtonColor:uint = 0xAAAAAA ;
		// over color of the default button.
		private var mDefaultButtonOverColor:uint = 0x888888 ;
		
		// array of WAKSimpleButtons
		private var mButtonsArray:Array = null ;
		
		// mouse cursor
		private var mMouseCursor:SciCursor = null ;
		
		// Inventory display
		private var mInventoryDisplay:SciInventoryDisplay = null ;
		
		// local reference to the resource manager.
		private var mResources:Class = null ;
		
		// Settings ////////////
		// height of the buttons panel.
		private var mPanelHeight:Number = 50 ;
		
		// color of the button panel
		private var mPanelColor:uint = 0xCCCCCC ;
		
		// Horizontal separation in pixels between each button.
		private var mButtonsSeparatorX:Number = 20 ;
		
		// Array of actions included as buttons.
		private var mActionsIncluded:Array = null ;
		
		// Asoc array of DisplayObjects of mouse icons, by action id. 
		private var mActionsMouseIcons:Array = null ;
		
		// panel image background.
		private var mPanelBg:DisplayObject = null ;
		
		// Default mouse cursor resource id.
		private var mDefaultMouseCursor:DisplayObject = null ;
		
		// use horizontal default layout for buttons.
		private var mUseHorizontalLayout:Boolean = true ;
		
		// horizontal layout vertical coordinate.
		private var mHorizontalLayoutY:Number = 0 ;
		
		// horizontal layout left margin space.
		private var mHorizontalLayoutLeftMargin:Number = 0 ;
		
		/// Inventory.///
		
		// inventory's number of rows
		private var mInventoryRows:Number = 2 ;
		
		// Inventory's number of columns.
		private var mInventoryCols:Number = 4 ;
		
		// Inventory's item width.
		private var mInventoryItemWidth:Number = 56 ;
		
		// Inventory's item height.
		private var mInventoryItemHeight:Number = 56 ;
		
		// Inventory's item x.
		private var mInventoryItemsX:Number = 56 ;
		
		// Inventory's item y.
		private var mInventoryItemsY:Number = 56 ;
		
		// Inventory's background.
		private var mInventoryBg:DisplayObject = null ;
		
		// inventory drop shadow effect enabled.
		private var mInventoryDropShadow:Boolean = false ;
		
		// Inventory's background color used if mInventoryBg is null.
		private var mInventoryBgColor:uint = 0x8395C8 ;
		
		// Inventory's border color used if mInventoryBg is null.
		private var mInventoryBorderColor:uint = 0x000000 ;
		
		// Inventory's window width used if mInventoryBg is null.
		private var mInventoryWidth:Number = 300 ;
		
		// Inventory's window height used if mInventoryBg is null.
		private var mInventoryHeight:Number = 200 ;
		
		// Actions array in the inventory.
		private var mActionsInventory:Array = null ;
		
		// Buttons array in the inventory.
		private var mButtonsInventory:Array = null ;
		
		// use horizontal default layout for buttons in the inventory.
		private var mInventoryUseHorizontalLayout:Boolean = true ;
		
		// horizontal layout vertical coordinate in the inventory.
		private var mInventoryHorizontalLayoutY:Number = 0 ;
		
		// horizontal layout left margin space for the inventory.
		private var mInventoryHorizontalLayoutLeftMargin:Number = 0 ;
		
		// Horizontal separation in pixels between each button in the inventory.
		private var mInventoryButtonsSeparatorX:Number = 20 ;
		
		
		private var mInventoryArrowUp:WakSimpleButton = null ;
		private var mInventoryArrowDown:WakSimpleButton = null ;
		///////////////////////
		
		// the background displayObject for the selected item.
		private var mSelectedItemBg:DisplayObject = null ;
		
		// The last action id selected.
		private var mLastActionSelected:String = "" ;
		
		//private var mLastEventTypeSent:String = "" ;
		
		// flag to know if a new action was just selected.
		private var mAllowShowPanel:Boolean = false ;
		
		// last selected inventory item
		private var mSelectedInventoryItem:InventoryItem = null ;
		
		private var mUseButton:SciItemSelectionButton = null ;
		
		// Saved action before open the inventory.
		private var mPrevInventoryAction:String = "" ;
		
		
		
		/**
		 * Constructor. 
		 * 
		 */		
		public function SciGui(pGuiSettings:XML = null)
		{
			super();
			mResources = AdventureMaster.Resources ;
			mButtonsArray = new Array();
			mActionsMouseIcons = new Array();
			mActionsInventory = new Array();
			mButtonsInventory = new Array();
			
			mBevelFilter1 = new BevelFilter(2, 45, 0xffffff, 1, 0x000000, 1, 3, 3, 1, BitmapFilterQuality.LOW, BitmapFilterType.INNER, false);
			
			LoadSettings(pGuiSettings);
			
			//mLastActionSelected = AdventureActions.ActionIdWalkTo ;
			
			// if not default cursor defined			
			if(mDefaultMouseCursor == null)
			    mDefaultMouseCursor = new ArrowIcon();
			
			mMouseCursor = new SciCursor(mDefaultMouseCursor);
			
			
			
			// if no action is included set the default actions.
			if(mActionsIncluded == null)
			{
				mActionsIncluded = new Array();
				mActionsIncluded.push( AdventureActions.ActionIdWalkTo );
				mActionsIncluded.push( AdventureActions.ActionIdLookAt );
				mActionsIncluded.push( AdventureActions.ActionIdInteract );
				mActionsIncluded.push( AdventureActions.ActionIdTalkTo );
				mActionsIncluded.push( OPEN_INVENTORY );
			}
			
			if(mActionsInventory.length == 0)
			{
				mActionsInventory.push( AdventureActions.ActionIdLookAt );
				mActionsInventory.push( AdventureActions.ActionIdInteract );
			}
			
			
			var fPlayerInventory:Inventory = AdventureMaster.CharactersMngr.GetPlayerCharacter().inventory ;
			// instanciate a SCI inventory display.
			mInventoryDisplay = new SciInventoryDisplay(fPlayerInventory, mActionsInventory, mInventoryRows, mInventoryCols,
					mInventoryItemWidth, mInventoryItemHeight, mInventoryBg, mInventoryBgColor, mInventoryBorderColor,
					mInventoryWidth, mInventoryHeight);
					
			mInventoryDisplay.mouseCursor = mMouseCursor ;
			
			mInventoryDisplay.dropShadow = mInventoryDropShadow ;
			mInventoryDisplay.SetActionsButtons(mButtonsInventory);
			mInventoryDisplay.horizontalLayout = mInventoryUseHorizontalLayout ;
			mInventoryDisplay.buttonsHorizontalSpace = mInventoryButtonsSeparatorX ;
			mInventoryDisplay.horizontalLayoutLeftMargin = mInventoryHorizontalLayoutLeftMargin ;
			mInventoryDisplay.horizontalLayoutY = mInventoryHorizontalLayoutY ;
			mInventoryDisplay.firstItemX = mInventoryItemsX ;
			mInventoryDisplay.firstItemY = mInventoryItemsY ;
			
			if(mInventoryArrowUp != null)
				mInventoryDisplay.arrowUp = mInventoryArrowUp ;
				
			if(mInventoryArrowDown != null)
				mInventoryDisplay.arrowDown = mInventoryArrowDown ;
			
			this.addEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
		}
		
		/**
		 * 
		 * @return true . If the gui is displaying the inventory. 
		 * 
		 */		
		public function ShowingInventory():Boolean
		{
			
			if(this.contains(mInventoryDisplay) && (mInventoryDisplay.visible) )
			{
				return true ;
			}
			
			return false ;
		}
		
		/**
		 * Shows the player's inventory. 
		 * 
		 */		
		public function ShowInventory():void
		{
			mPrevInventoryAction = mLastActionSelected ;
			this.addChild( mInventoryDisplay );
			AdventureMaster.SceneMngr.enabled = false ;
			mButtonsPanel.visible = false ;
			
			mInventoryDisplay.addEventListener(GuiEvent.ACTION_SELECTED, OnInventoryActionSelected);
			mInventoryDisplay.addEventListener(GuiEvent.INVENTORY_ITEM_SELECTED, OnInventoryItemSelected);
			
		}
		
		/**
		 * Hide the player's inventory. 
		 * 
		 */		
		public function HideInventory():void
		{
			if(this.contains( mInventoryDisplay ) )
				this.removeChild( mInventoryDisplay );
				
			AdventureMaster.SceneMngr.enabled = true ;
			
			mInventoryDisplay.removeEventListener(GuiEvent.ACTION_SELECTED, OnInventoryActionSelected);
			mInventoryDisplay.removeEventListener(GuiEvent.INVENTORY_ITEM_SELECTED, OnInventoryItemSelected);
			
			if( !mInventoryDisplay.itemWasSelected )
			{
				SetActionSelected(mPrevInventoryAction,true);
			}
		}
		
		private function DrawDefaultUseItemBg(pColor:uint):Sprite
		{
			var fBg:Sprite = new Sprite();
			fBg.graphics.beginFill(pColor);
			fBg.graphics.drawRect(0,0,mInventoryItemWidth + 2, 45 );
			fBg.graphics.endFill() ; 
			
			return fBg ;
		}
		
		
		protected function LoadSettings(pSettings:XML):void
		{
			if( pSettings.PanelColor != "" )
			{
				mPanelColor = uint("0x"+pSettings.PanelColor) ;
			}
			
			if( pSettings.PanelBg != "" )
			{
				mPanelBg = new mResources[pSettings.PanelBg] ;
			}
			
			if( pSettings.DefaultMouseCursor != "" )
			{
				mDefaultMouseCursor = new mResources[pSettings.DefaultMouseCursor] ;
				if(pSettings.DefaultMouseCursor.@x != "")
					mDefaultMouseCursor.x = pSettings.DefaultMouseCursor.@x ;
					
				if(pSettings.DefaultMouseCursor.@y != "")
					mDefaultMouseCursor.y = pSettings.DefaultMouseCursor.@y ;
			}
			
			if( pSettings.PanelHeight != "" )
			{
				mPanelHeight = pSettings.PanelHeight ;
			}
			
			if( pSettings.ButtonsHorizontalSpace != "" )
			{
				mButtonsSeparatorX = pSettings.ButtonsHorizontalSpace ;
			}
			
			if( pSettings.UseHorizontalLayout != "" )
			{
				mUseHorizontalLayout = pSettings.UseHorizontalLayout == "true"? true : false ;
			}
			
			if( pSettings.HorizontalLayoutY != "" )
			{
				mHorizontalLayoutY = pSettings.HorizontalLayoutY ;
			}
			
			if( pSettings.HorizontalLayoutLeftMargin != "" )
			{
				mHorizontalLayoutLeftMargin = pSettings.HorizontalLayoutLeftMargin ;
			}
			
			
			
			
			// Load actions/buttons
			var fDefaultBtn:WakSimpleButton = null ;
			
			for each( var button:XML in pSettings.Button)
			{
				if(mActionsIncluded == null)
					mActionsIncluded = new Array();
				
				
				mActionsIncluded.push(button.@action);
				
				if( (button.Up != "") && (button.Over != "") && (button.Down != "") )
				{
					CreateCustomButton(button, mButtonsArray, mUseHorizontalLayout);
				}
				else
				{
					fDefaultBtn = CreateButton(button.@action);
					mButtonsArray.push(fDefaultBtn);
				}
			}
			
			// inventory settings
			
			// background image
			if( pSettings.Inventory.Background != "" )
			{
				mInventoryBg = new mResources[pSettings.Inventory.Background] ;
			}
			
			// rows of items
			if( pSettings.Inventory.Rows != "")
			{
				mInventoryRows = pSettings.Inventory.Rows ;
			}
			
			// columns of items
			if( pSettings.Inventory.Cols != "")
			{
				mInventoryCols = pSettings.Inventory.Cols ;
			}
			
			// item width
			if( pSettings.Inventory.ItemWidth != "")
			{
				mInventoryItemWidth = pSettings.Inventory.ItemWidth ;
			}
			
			// item height
			if( pSettings.Inventory.ItemHeight != "")
			{
				mInventoryItemHeight = pSettings.Inventory.ItemHeight ;
			}
			
			// items X
			if( pSettings.Inventory.ItemsX != "")
			{
				mInventoryItemsX = pSettings.Inventory.ItemsX ;
			}
			
			// items Y
			if( pSettings.Inventory.ItemsY != "")
			{
				mInventoryItemsY = pSettings.Inventory.ItemsY ;
			}
			
			// Inventory's background color used if mInventoryBg is null.
			if( pSettings.Inventory.BgColor != "")
			{
				mInventoryBgColor = uint("0x"+pSettings.Inventory.BgColor) ;
			}
			 
			
			// Inventory's border color used if mInventoryBg is null.
			if( pSettings.Inventory.BorderColor != "")
			{
				mInventoryBorderColor = uint("0x"+pSettings.Inventory.BorderColor) ;
			}
			
			
			// Inventory's window width used if mInventoryBg is null.
			if( pSettings.Inventory.Width != "")
			{
				mInventoryWidth = pSettings.Inventory.Width ;
			}
			
			// Inventory's window height used if mInventoryBg is null.
			if( pSettings.Inventory.Height != "")
			{
				mInventoryHeight = pSettings.Inventory.Height ;
			}
			
			// Inventory's window height used if mInventoryBg is null.
			if( pSettings.Inventory.DropShadow != "")
			{
				mInventoryDropShadow = pSettings.Inventory.DropShadow == "true" ? true : false ;
			}
			
			if( pSettings.Inventory.ButtonsHorizontalSpace != "" )
			{
				mInventoryButtonsSeparatorX = pSettings.Inventory.ButtonsHorizontalSpace ;
			}
			
			if( pSettings.Inventory.UseHorizontalLayout != "" )
			{
				mInventoryUseHorizontalLayout = pSettings.Inventory.UseHorizontalLayout == "true"? true : false ;
			}
			
			if( pSettings.Inventory.HorizontalLayoutY != "" )
			{
				mInventoryHorizontalLayoutY = pSettings.Inventory.HorizontalLayoutY ;
			}
			
			if( pSettings.Inventory.HorizontalLayoutLeftMargin != "" )
			{
				mInventoryHorizontalLayoutLeftMargin = pSettings.Inventory.HorizontalLayoutLeftMargin ;
			}
			
			// Loads inventory buttons.
			for each( var invbutton:XML in pSettings.Inventory.Button)
			{
				//trace("inventory btn:"+ invbutton.toXMLString());
				
				if( (invbutton.Up != "") && (invbutton.Over != "") && (invbutton.Down != "") )
				{
					CreateCustomButton(invbutton, mButtonsInventory, mInventoryUseHorizontalLayout);
				}
				else
				{
					fDefaultBtn = CreateButton(invbutton.@action);
					mButtonsArray.push(fDefaultBtn);
				}
			}
			
			var fScrollUpBtn:XMLList = pSettings.Inventory.ScrollUpButton ;
			if( (fScrollUpBtn.Up != "") && (fScrollUpBtn.Down != "") && (fScrollUpBtn.Over != "") )
			{
				mInventoryArrowUp = CreateScrollCustomButton(fScrollUpBtn);
				mInventoryArrowUp.x = fScrollUpBtn.Position.@x ;
				mInventoryArrowUp.y = fScrollUpBtn.Position.@y ;
			}
			
			var fScrollDownBtn:XMLList = pSettings.Inventory.ScrollDownButton ;
			if( (fScrollDownBtn.Up != "") && (fScrollDownBtn.Down != "") && (fScrollDownBtn.Over != "") )
			{
				mInventoryArrowDown = CreateScrollCustomButton(fScrollDownBtn);
				mInventoryArrowDown.x = fScrollDownBtn.Position.@x ;
				mInventoryArrowDown.y = fScrollDownBtn.Position.@y ;
			}
			
			
			
			
			
			
			
			
			
			
		}
		
		/**
		 * Listener for the event ADDED_TO_STAGE. 
		 * @param event
		 * 
		 */		
		protected function OnAddedToStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
			
			mStageWidth = this.stage.stageWidth ;
			mStageHeight = this.stage.stageHeight ;
			
			// align the inventory display
			mInventoryDisplay.x = Utils.AlignCenter( mInventoryDisplay.width, mStageWidth );
			mInventoryDisplay.y = Utils.AlignCenter( mInventoryDisplay.height, mStageHeight );
			
			if(mButtonsPanel == null)
			{
				mButtonsPanel = new Sprite();
				if( mPanelBg == null)
				{
					mButtonsPanel.graphics.beginFill( mPanelColor );
					mButtonsPanel.graphics.drawRect(0,0, mStageWidth, mPanelHeight );
					mButtonsPanel.graphics.endFill() ;
				}
				else
				{
					mButtonsPanel.addChild(mPanelBg);
				}
			}
			
			
			mButtonsPanel.visible = false ;
			
			this.addChild(mButtonsPanel);
			
			mButtonsPanel.addEventListener(MouseEvent.MOUSE_OUT, OnButtonPanelMouseOut );
			
			var fButtonX:Number = mHorizontalLayoutLeftMargin ;
			for each( var button:WakSimpleButton in mButtonsArray )
			{
				if(mUseHorizontalLayout)
				{
					button.y = mHorizontalLayoutY ;
					button.x = fButtonX ;
					fButtonX += button.width + mButtonsSeparatorX ;
				}
				mButtonsPanel.addChild(button);
				button.addEventListener(MouseEvent.MOUSE_UP,OnActionSelected);
			}
			//mUseButton.y = mHorizontalLayoutY ;
			//mUseButton.x = fButtonX ;
			//mButtonsPanel.addChild(mUseButton);
			
			var fPlayerInventory:Inventory = AdventureMaster.CharactersMngr.GetPlayerCharacter().inventory ;
			fPlayerInventory.addEventListener(InventoryEvent.ITEM_REMOVED, mUseButton.OnItemRemoved );
			mUseButton.addEventListener(GuiEvent.INVENTORY_ITEM_SELECTED,OnUseInventoryItem);
			
			
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);
			
			SetInventoryDisplayListeners( mInventoryDisplay );
			
			this.addEventListener(Event.REMOVED_FROM_STAGE,OnRemovedToStage);
		}
		
		/**
		 * Listener for the event REMOVED_FROM_STAGE. 
		 * @param event
		 * 
		 */		
		protected function OnRemovedToStage(event:Event):void
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,OnRemovedToStage);
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);
			
			for each( var button:WakSimpleButton in mButtonsArray )
			{
				button.addEventListener(MouseEvent.MOUSE_UP,OnActionSelected);
			}
			
			mButtonsPanel.removeEventListener(MouseEvent.MOUSE_OUT, OnButtonPanelMouseOut );
			
			var fPlayerInventory:Inventory = AdventureMaster.CharactersMngr.GetPlayerCharacter().inventory ;
			fPlayerInventory.removeEventListener(InventoryEvent.ITEM_REMOVED, mUseButton.OnItemRemoved );
			mUseButton.removeEventListener(GuiEvent.INVENTORY_ITEM_SELECTED,OnUseInventoryItem);
			
			RemoveInventoryDisplayListeners( mInventoryDisplay );
			this.addEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
		}
		
		/**
		 * Listener for the event MOUSE_MOVE. 
		 * @param event
		 * 
		 */		
		protected function OnMouseMove(event:MouseEvent):void
		{
			mMouseCursor.x = event.stageX ;
			mMouseCursor.y = event.stageY ;
			
			if( ShowingInventory() )
				return ;
			
			if(!mEnabled)
				return ;
			 
			if((event.stageY < (mPanelHeight/2)) && (mAllowShowPanel))
			{
				if(!mButtonsPanel.visible)
				{
					mButtonsPanel.visible = true ;
					mMouseCursor.SetDefault() ;
				}				
			}
			
			if( event.stageY > (mPanelHeight/2) )
			{
				mAllowShowPanel = true ;
			}
			
			
			if(event.stageY > (mPanelHeight) )
			{
				if(mButtonsPanel.visible)
					mButtonsPanel.visible = false ;
					
				if( mMouseCursor.isDefault )
				{
					SetMouseIconAction(mLastActionSelected);
					
				}
			}
				
			/* 
			else
			{
				mButtonsPanel.visible = false ;
				SetMouseIconAction(mLastActionSelected);
			}
			 */ 
			/* 
			// update the wak buttons.
			for each(var button:WakSimpleButton in mButtonsArray)
			{
				button.Update( event.stageX, event.stageY);
			}
			 */
		}
		
		protected function OnButtonPanelMouseOut(event:MouseEvent):void
		{
			//trace("OnButtonPanelMouseOut");
			if(!mEnabled)
				return ;
			
			if(event.stageY < mButtonsPanel.height )
				return ;
			
			mButtonsPanel.visible = false ;
			SetMouseIconAction(mLastActionSelected);
		}
		
		protected function SetInventoryDisplayListeners( pInvDisplay:SciInventoryDisplay ):void
		{
			pInvDisplay.addEventListener(GuiEvent.INVENTORY_CLOSED,OnInventoryOk);
			//pInvDisplay.addEventListener(GuiEvent.INVENTORY_ITEM_OUT, OnInventoryItemOut );
			//pInvDisplay.addEventListener(GuiEvent.INVENTORY_ITEM_OVER, OnInventoryItemOver );
			//pInvDisplay.addEventListener(GuiEvent.INVENTORY_ITEM_SELECTED, OnInventoryItemClick );
		}
		
		protected function RemoveInventoryDisplayListeners( pInvDisplay:SciInventoryDisplay ):void
		{
			pInvDisplay.removeEventListener(GuiEvent.INVENTORY_CLOSED,OnInventoryOk);
			//pInvDisplay.removeEventListener(GuiEvent.INVENTORY_ITEM_OUT, OnInventoryItemOut );
			//pInvDisplay.removeEventListener(GuiEvent.INVENTORY_ITEM_OVER, OnInventoryItemOver );
			//pInvDisplay.removeEventListener(GuiEvent.INVENTORY_ITEM_SELECTED, OnInventoryItemClick );
		}
		
		protected function OnInventoryOk(event:Event):void
		{
			HideInventory();
		}
		
		/**
		 * @inheritDoc
		 */		
		public function get display():DisplayObject
		{
			return this;
		}
		
		/**
		 * @inheritDoc 
		 */		
		public function SetMouseCursor(pCursorLayer:Sprite):void
		{
			Mouse.hide() ;
			
			pCursorLayer.addChild(mMouseCursor);
			
			CreateDefaultMouseIcons(mActionsMouseIcons, mActionsIncluded, pCursorLayer);
		}
		
		/**
		 * @inheritDoc
		 */		
		public function RemoveMouseCursor(pCursorLayer:Sprite):void
		{
		}
		
		/**
		 * @inheritDoc
		 */		
		public function set enabled(value:Boolean):void
		{
			mEnabled = value ;
			
			if(!mEnabled)
			{
				mButtonsPanel.visible = false ;
				mAllowShowPanel = false ;
				mMouseCursor.SetDefault() ;
			}
			else
			{
				SetMouseIconAction(mLastActionSelected);
			}
		}
		
		public function get enabled():Boolean
		{
			return mEnabled;
		}
		
		
		
		protected function OnInventoryItemSelected(event:GuiEvent):void
		{
			//trace("SciGui OnInventoryItemSelected");
			//mSelectedInventoryItem = event.inventoryItem ;
			//mUseButton.SetItemSelected( mSelectedInventoryItem );
			//mActionsMouseIcons[AdventureActions.ActionIdUse] = mSelectedInventoryItem.itemIconDisplay ;
			
			var fEvent:GuiEvent = null ;
			
			fEvent = new GuiEvent(event.type,event.buttonId,event.inventoryItem);
			
			this.dispatchEvent(fEvent);
			
			mLastActionSelected = AdventureActions.ActionIdUse ;
		}
		
		protected function OnInventoryActionSelected(event:GuiEvent):void
		{
			
			if(event.buttonId == AdventureActions.ActionIdUse)
			{
				mSelectedInventoryItem = event.inventoryItem ;
				mUseButton.SetItemSelected( event.inventoryItem );
			}
			else
			{
				SetMouseIconAction( event.buttonId );
			}
			
			//SetActionSelected(event.buttonId,fSetMouseIcon);
			
			//trace("SciGui OnInventoryActionSelected:"+event.buttonId);
			
			mLastActionSelected = event.buttonId ;
			var fEvent:Event = new GuiEvent(event.type, event.buttonId,event.inventoryItem);
			this.dispatchEvent(fEvent);
			
		}
		
		protected function OnActionSelected(event:Event):void
		{
			//trace("SciGui OnActionSelected t:"+event.target + " ct:"+event.currentTarget);
			var fButton:WakSimpleButton = event.currentTarget as WakSimpleButton ;
			
			if( fButton != null )
			{
				SetActionSelected(fButton.buttonName);
			}
		}
		
		
		protected function SetActionSelected(pButtonName:String, pSetMouse:Boolean = true ):void
		{
			if(pSetMouse)
				SetMouseIconAction( pButtonName );
			
			//trace("SciGui SetActionSelected:"+pButtonName);
			
			// if this a common action	
			if((pButtonName != OPEN_INVENTORY))
			{
				mLastActionSelected = pButtonName ;
				mButtonsPanel.visible = false ;
				mAllowShowPanel = false ;
				//trace("SetActionSelected:"+pButtonName);
				var fEvent:GuiEvent = null ;
				if(pButtonName != AdventureActions.ActionIdUse)
				{
					fEvent = new GuiEvent(GuiEvent.ACTION_SELECTED, pButtonName,null);
					
				}
				else if( mUseButton.selectedItem == null)
				{
					mAllowShowPanel = false ;
					ShowInventory();
				}				
				
				if(fEvent != null)
					this.dispatchEvent(fEvent);
				
			} // or the action is open the inventory or the use item with an empty item was clicked.
			else if(pButtonName == OPEN_INVENTORY )
			{
				mAllowShowPanel = false ;
				ShowInventory();
			}
		}
		
		protected function OnUseInventoryItem(event:GuiEvent):void
		{
			if(event.inventoryItem == null )
			{
				ShowInventory();
			}
			else
			{
				
				mButtonsPanel.visible = false ;
				mAllowShowPanel = false ;
				
				mLastActionSelected = AdventureActions.ActionIdUse ;
				SetMouseIconAction( mLastActionSelected );
				mSelectedInventoryItem = event.inventoryItem ;
				
				var fActionEvent:GuiEvent = new GuiEvent(GuiEvent.ACTION_SELECTED,AdventureActions.ActionIdUse,mSelectedInventoryItem);
				this.dispatchEvent(fActionEvent);
			}
		}
		
		/**
		 * Set the mouse icon to certain action. 
		 * @param pActionId action id.
		 * 
		 */		
		public function SetMouseIconAction(pActionId:String):void
		{
			
			if( pActionId == OPEN_INVENTORY || ( (pActionId == mUseButton.buttonName) && (mUseButton.selectedItem == null) )  )
				return ;
				
			//trace("SciGui SetMouseIconAction:"+pActionId);
			
			var fIcon:DisplayObject = null ;
			var fMarkPoint:Boolean = false ;
			
			if(pActionId == AdventureActions.ActionIdUse )
			{	
				fIcon = mSelectedInventoryItem.itemIconDisplay ;
				fMarkPoint = true ;
			}
			else
			{
				fIcon = mActionsMouseIcons[pActionId] ;
			}
			
			mLastActionSelected = pActionId ;
			
			mMouseCursor.SetActiveIcon( fIcon, fMarkPoint );
			
		}
		
		protected function CreateDefaultMouseIcons(pIconsArr:Array, pActionsArr:Array, pCursorLayer:DisplayObjectContainer):void
		{
			var fIcon:Sprite = null ;
			for each( var action:String in pActionsArr )
			{
				fIcon = new Sprite();
				if( action != OPEN_INVENTORY)
					DrawActionIcon(fIcon,action);
					
				fIcon.scaleX = 0.5 ;
				fIcon.scaleY = 0.5 ;
				if(pIconsArr[action] == null )
				{
					pIconsArr[action] = fIcon ;
				}
				fIcon.mouseEnabled = false ;
			}
		}
		
		protected function CreateButtons(pActionsArr:Array, pButtonsArr:Array):void
		{
			var fNewButton:WakSimpleButton = null ;
			for each( var action:String in pActionsArr)
			{
				fNewButton = CreateButton(action);				
				pButtonsArr.push(fNewButton);
			}
		}
		
		protected function CreateButton(pAction:String):WakSimpleButton
		{
			var fButton:WakSimpleButton = null ;
			
			var fUp:Sprite = new Sprite();
			DrawDefaultButtonBg(fUp, mDefaultButtonWidth, mDefaultButtonHeight, mDefaultButtonColor);
			DrawActionIcon(fUp,pAction);
			
			var fOver:Sprite = new Sprite();
			DrawDefaultButtonBg(fOver, mDefaultButtonWidth, mDefaultButtonHeight, mDefaultButtonOverColor);
			DrawActionIcon(fOver,pAction);
			
			var fDown:Sprite = new Sprite();
			DrawDefaultButtonBg(fDown, mDefaultButtonWidth, mDefaultButtonHeight, mDefaultButtonOverColor);
			DrawActionIcon(fDown,pAction);
			
			if(pAction != AdventureActions.ActionIdUse)
			{
				fButton = new WakSimpleButton( fUp, fOver, fDown, pAction);
				fButton.filters = [mBevelFilter1] ;
			}
			else
			{
				
				mUseButton = new SciItemSelectionButton( DrawDefaultUseItemBg(0x000000), DrawDefaultUseItemBg(0x000000), DrawDefaultUseItemBg(0x000000),0, pAction);
				fButton = mUseButton;
			}
			
			
			return fButton ;
		}
		
		protected function DrawDefaultButtonBg(pBtn:Sprite, pW:Number, pH:Number, pColor:uint):void
		{
			pBtn.graphics.beginFill(pColor);
			pBtn.graphics.drawRoundRect(0,0,pW,pH,10,10);
			pBtn.graphics.endFill();
		}
		
		//TODO: make the icons from xml parameter.
		public static function DrawActionIcon(pBtn:Sprite, pAction:String):void
		{
			var fNewIcon:Sprite = null ;
			switch(pAction)
			{
				case AdventureActions.ActionIdWalkTo:
					fNewIcon = new WalkIcon();
				break ;
				
				case AdventureActions.ActionIdLookAt:
					fNewIcon = new LookIcon();
				break ;
				case AdventureActions.ActionIdInteract:
					fNewIcon = new InteractIcon();
				break ;
				
				case AdventureActions.ActionIdTalkTo:
					fNewIcon = new TalkIcon();
				break ;
				case OPEN_INVENTORY:
					fNewIcon = new InventoryIcon();
				break ;
				default:
					fNewIcon = new ArrowIcon();
				break;
			}
			fNewIcon.mouseEnabled = false ;
			fNewIcon.mouseChildren = false ;
			
			fNewIcon.x = Utils.AlignCenter(fNewIcon.width, pBtn.width); 
			fNewIcon.y = Utils.AlignCenter(fNewIcon.height, pBtn.height);
			
			pBtn.addChild(fNewIcon);
		}
		
		public function get mouseCursor():DisplayObject 
		{
			return mMouseCursor ;
		}
		
		protected function CreateScrollCustomButton(pButtonXml:XMLList):WakSimpleButton
		{
			var fButton:WakSimpleButton = null ;
			var fUp:DisplayObject = new mResources[pButtonXml.Up]  ;
			var fOver:DisplayObject = new mResources[pButtonXml.Over]  ;
			var fDown:DisplayObject = new mResources[pButtonXml.Down]  ;
			
			var fButtonId:String = pButtonXml.@action ;
			fButton = new WakSimpleButton(fUp,fOver,fDown, fButtonId);
			return fButton;
		}
		
		
		protected function CreateCustomButton(pButtonXml:XML, pSaveArray:Array, pUseHorizontal:Boolean):void
		{
			var fButton:WakSimpleButton = null ;
			var fBtnX:Number = 0 ;
			var fBtnY:Number = 0 ;
			
			var fUp:DisplayObject = new mResources[pButtonXml.Up]  ;
			var fOver:DisplayObject = new mResources[pButtonXml.Over]  ;
			var fDown:DisplayObject = new mResources[pButtonXml.Down]  ;
			
			var fButtonId:String = pButtonXml.@action ;
			
			if(fButtonId == "")
			{
				return ;
			}
			
		
			if(fButtonId != AdventureActions.ActionIdUse)
			{
				fButton = new WakSimpleButton(fUp,fOver,fDown, fButtonId);
				if((pButtonXml.hasOwnProperty("MouseIcon")) && (pButtonXml.MouseIcon != ""))
				{
					//trace("creating icon:"+pButtonXml.MouseIcon)
					var fIcon:DisplayObject = null;
					fIcon = new mResources[pButtonXml.MouseIcon] ;
					fIcon.x = pButtonXml.MouseIcon.@x ;
					fIcon.y = pButtonXml.MouseIcon.@y ;
					mActionsMouseIcons[fButtonId] = fIcon ;
				}
			}
			else
			{
				var fMaskMargin:int = 0 ;
				if(pButtonXml.MaskMargin != "")
					fMaskMargin = pButtonXml.MaskMargin ;
				
				mUseButton = new SciItemSelectionButton(fUp, fOver, fDown, fMaskMargin, fButtonId);
				fButton = mUseButton;
			}
			
			if(!pUseHorizontal)
			{
				if( (pButtonXml.Position.@x != "") && (pButtonXml.Position.@y != "") )
				{
					fButton.x = pButtonXml.Position.@x ;
					fButton.y = pButtonXml.Position.@y ;
				}
			}
			
			pSaveArray.push(fButton);
		}
		
		
		
	}
}